package cn.edu.njust.alg.leetcode;

/**
 * 创建时间：2015年6月15日 上午11:47:37 项目名称：AlgorithmExercise
 * 
 * @author Zhou Xiumin
 * @version 1.0
 * @since JDK 1.7.0_21 文件名称：Leetcode_0_t.java 类说明：
 */
public class Leetcode_110_Balanced_Binary_Tree {

	public static Info dfs(TreeNode p) {
		Info info = new Info(0, true);
		if (p == null) {
			return info;
		}
		Info left = dfs(p.left);
		Info right = dfs(p.right);
		if (!left.isBalanced || !right.isBalanced
				|| (int) Math.abs(left.height - right.height) > 1) {
			info.isBalanced = false;
		}
		info.height = Math.max(left.height, right.height) + 1;

		return info;
	}

	public static boolean isBalanced(TreeNode root) {
		Info info = dfs(root);
		return info.isBalanced;
	}

	public static void main(String[] args) {

	}

}

class Info {
	int height;
	boolean isBalanced;

	public Info(int height, boolean isBalanced) {
		this.height = height;
		this.isBalanced = isBalanced;
	}

}
